﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CoreEx.Database.MetaData;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
namespace CoreEx.Database.Metadata.SqlServerProvider
{
    [Implements(typeof(IMetadataProvider), ServiceName = "System.Data.SqlClient")]
    public class SqlServerProvider : MetadataProvider
    {

        
        public override IEnumerable<ProviderTableInfo> GetTables()
        {
            var reader = ExecuteReader(Sql.Tables);
            IList<ProviderTableInfo> tables = new List<ProviderTableInfo>();            
            while(reader.Read())
            {
                var tableInfo = new ProviderTableInfo();
                tableInfo.TableCatalog = reader.GetString(0);
                tableInfo.TableSchema = reader.GetString(1);
                tableInfo.TableName = reader.GetString(2);
                tables.Add(tableInfo);
            }
            reader.Close();
            return tables;
        }
       
        public override IEnumerable<ProviderColumnInfo> GetColumns()
        {
            var reader = ExecuteReader(Sql.Columns);
            IList<ProviderColumnInfo> columns = new List<ProviderColumnInfo>();
            while (reader.Read())
            {
                var columnInfo = new ProviderColumnInfo();
                columnInfo.TableCatalog = reader.GetString(0);
                columnInfo.TableSchema = reader.GetString(1);
                columnInfo.TableName = reader.GetString(2);
                columnInfo.ColumnName = reader.GetString(3);
                columnInfo.OrdinalPosition = reader.GetInt32(4);
                columnInfo.DataTypeName = reader.GetString(5);
                columnInfo.IsNullable = reader.GetInt32(6) == 1 ? true : false;
                columns.Add(columnInfo);
            }
            reader.Close();
            return columns;
        }

        public override IEnumerable<ProviderForeignKeyInfo> GetForeignKeys()
        {
            var reader = ExecuteReader(Sql.ForeignKeys);
            IList<ProviderForeignKeyInfo> foreignKeys = new List<ProviderForeignKeyInfo>();
            while (reader.Read())
            {
                var foreignkeyInfo = new ProviderForeignKeyInfo();
                foreignkeyInfo.ConstraintName = reader.GetString(0);
                foreignkeyInfo.ForeignTableCatalog = reader.GetString(1);
                foreignkeyInfo.ForeignTableSchema = reader.GetString(2);
                foreignkeyInfo.ForeignTableName = reader.GetString(3);
                foreignkeyInfo.ForeignColumnName = reader.GetString(4);
                foreignkeyInfo.PrimaryTableCatalog = reader.GetString(5);
                foreignkeyInfo.PrimaryTableSchema = reader.GetString(6);
                foreignkeyInfo.PrimaryTableName = reader.GetString(7);
                foreignkeyInfo.PrimaryColumnName = reader.GetString(8);
                foreignKeys.Add(foreignkeyInfo);
            }
            reader.Close();
            return foreignKeys;
        }

        public override IEnumerable<ProviderPrimaryKeyInfo> GetPrimaryKeys()
        {
             var reader = ExecuteReader(Sql.PrimaryKeys);
            IList<ProviderPrimaryKeyInfo> primaryKeys = new List<ProviderPrimaryKeyInfo>();
            while (reader.Read())
            {
                var primaryKeyInfo = new ProviderPrimaryKeyInfo();
                primaryKeyInfo.ConstraintName = reader.GetString(0);
                primaryKeyInfo.TableCatalog = reader.GetString(1);
                primaryKeyInfo.TableSchema = reader.GetString(2);
                primaryKeyInfo.TableName = reader.GetString(3);
                primaryKeyInfo.ColumnName = reader.GetString(4);
                primaryKeys.Add(primaryKeyInfo);
            }
            reader.Close();
            return primaryKeys;
        }
    }
}
